<?xml version="1.0" encoding="utf-8" ?>

<!--
Copyright (C) 2010 Calin Radoni

This file is part of CHScanner.

CHScanner is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CHScanner is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
-->

<!-- This is the XSL for normal pages -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="html" encoding="UTF-8"/>

	<xsl:include href="header.xsl"/>

	<xsl:template match="Document">
		<xsl:apply-templates select="PageBody"/>
    <xsl:apply-templates select="firstPage"/>
    <xsl:apply-templates select="article"/>
		<xsl:apply-templates select="book"/>
		<xsl:apply-templates select="chapter"/>
		<xsl:apply-templates select="preface"/>
		<xsl:apply-templates select="appendix"/>
	</xsl:template>

<!-- PageBody -->
	<xsl:template match="PageBody">
		<xsl:if test="count(section) &gt; 1">
			<p><span class="pageBodyTOC">
			<h2>Table of Contents</h2>
			<dl>
				<xsl:for-each select="section">
					<dt>
						<a class="pageLink">
							<xsl:attribute name="href">
								<xsl:text>#</xsl:text>
								<xsl:value-of select="@id"/>
							</xsl:attribute>
							<xsl:attribute name="target">_self</xsl:attribute>
							<xsl:value-of select="title"/>
						</a>
					</dt>
				</xsl:for-each>
			</dl>
			</span></p>
			<hr class="Hr70"/>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>

<!-- firstPage -->
  <xsl:template match="firstPage">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
  
<!-- article -->
	<xsl:template match="article">
		<xsl:apply-templates select="title" mode="article"/>
		<xsl:if test="count(sect1) &gt; 1">
			<p><span class="articleTOC">
			<h2>Table of Contents</h2>
			<dl>
				<xsl:for-each select="sect1">
					<dt>
						<a class="pageLink">
							<xsl:attribute name="href">
								<xsl:text>#</xsl:text>
								<xsl:value-of select="@id"/>
							</xsl:attribute>
							<xsl:attribute name="target">_self</xsl:attribute>
							<xsl:value-of select="title"/>
						</a>
					</dt>
				</xsl:for-each>
			</dl>
			</span></p>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>
	
<!-- book -->
	<xsl:template match="book">
		<xsl:apply-templates select="title" mode="article"/>
		
		<xsl:if test="count(Document/chapter) &gt; 1">
			<p><span class="bookTOC">
			<h2>Table of Contents</h2>
			<dl>
				<xsl:if test="Document/preface">
					<dt>
						<a class="pageLink">
							<xsl:attribute name="href">
								<xsl:text>preface.html</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="target">_self</xsl:attribute>
							<xsl:value-of select="Document/preface/title"/>
						</a>
					</dt>
				</xsl:if>
				
				<xsl:for-each select="Document/chapter">
					<dt>
						<a class="pageLink">
							<xsl:attribute name="href">
								<xsl:value-of select="@filename"/>
								<xsl:text>.html</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="target">_self</xsl:attribute>
							<xsl:value-of select="title"/>
						</a>
						
						<xsl:variable name="chapterFileName">
							<xsl:value-of select="@filename"/>
						</xsl:variable>
						
						<xsl:if test="count(sect1) &gt; 0">
							<dd><dl>
								<xsl:for-each select="sect1">
									<dt>
										<a class="pageLink">
											<xsl:attribute name="href">
												<xsl:copy-of select="$chapterFileName"/>
												<xsl:text>.html#</xsl:text>
												<xsl:value-of select="@id"/>
											</xsl:attribute>
											<xsl:attribute name="target">_self</xsl:attribute>
											<xsl:value-of select="title"/>
										</a>
									</dt>
								</xsl:for-each>
							</dl></dd>
						</xsl:if>
					</dt>
				</xsl:for-each>

				<xsl:for-each select="Document/appendix">
					<dt>
						<a class="pageLink">
							<xsl:attribute name="href">
								<xsl:value-of select="@filename"/>
								<xsl:text>.html</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="target">_self</xsl:attribute>
							<xsl:text>Appendix </xsl:text>
							<xsl:value-of select="title"/>
						</a>
					</dt>
				</xsl:for-each>
			</dl>
			</span></p>
		</xsl:if>
		
		<xsl:apply-templates select="*[not(self::Document/preface|self::Document/chapter|self::Document/appendix)]"/>
	</xsl:template>
	
<!-- chapter -->
	<xsl:template match="chapter">
		<a>
			<xsl:attribute name="name">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
		</a>

		<xsl:apply-templates select="title" mode="section"/>

		<xsl:if test="count(sect1) &gt; 1">
			<p><span class="chapterTOC">
			<!-- <h3>Table of Contents</h3> -->
			<dl>
				<xsl:for-each select="sect1">
					<dt>
						<a class="pageLink">
							<xsl:attribute name="href">
								<xsl:text>#</xsl:text>
								<xsl:value-of select="@id"/>
							</xsl:attribute>
							<xsl:attribute name="target">_self</xsl:attribute>
							<xsl:value-of select="title"/>
						</a>
					</dt>
				</xsl:for-each>
			</dl>
			</span></p>
		</xsl:if>

		<xsl:apply-templates/>
	</xsl:template>

<!-- preface -->
	<xsl:template match="preface">
		<xsl:if test="title">
			<xsl:apply-templates select="title" mode="simplesection"/>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>

<!-- appendix -->
	<xsl:template match="appendix">
		<xsl:if test="title">
			<xsl:apply-templates select="title" mode="appendix"/>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>

<!-- simplesection -->
	<xsl:template match="simplesection">
		<xsl:if test="title">
			<xsl:apply-templates select="title" mode="simplesection"/>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>
	
<!-- section -->
	<xsl:template match="section">
		<a>
			<xsl:attribute name="name">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
		</a>
		<xsl:apply-templates select="title" mode="section"/>
		<xsl:apply-templates/>
	</xsl:template>
	
<!-- sect1 -->
	<xsl:template match="sect1">
		<a>
			<xsl:attribute name="name">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
		</a>
		<xsl:apply-templates select="title" mode="sect1"/>
		<xsl:apply-templates/>
	</xsl:template>
	
<!-- sect2 -->
	<xsl:template match="sect2">
		<a>
			<xsl:attribute name="name">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
		</a>
		<xsl:apply-templates select="title" mode="sect2"/>
		<xsl:apply-templates/>
	</xsl:template>
	
<!-- titles -->
	<xsl:template match="title"/>
	
	<xsl:template match="title" mode="article">
		<p class="articleTitle">
			<xsl:value-of select="."/>
		</p>
	</xsl:template>
	
	<xsl:template match="title" mode="appendix">
		<h2>
      <xsl:text>Appendix </xsl:text>
      <xsl:value-of select="."/>
    </h2>
	</xsl:template>

  <xsl:template match="title" mode="simplesection">
		<h2><xsl:value-of select="."/></h2>
	</xsl:template>
	
	<xsl:template match="title" mode="section">
		<h2><xsl:value-of select="."/></h2>
	</xsl:template>
	
	<xsl:template match="title" mode="sect1">
		<h2><xsl:value-of select="."/></h2>
	</xsl:template>
	
	<xsl:template match="title" mode="sect2">
		<h3><xsl:value-of select="."/></h3>
	</xsl:template>

<!-- elements -->
	<xsl:template match="br">
		<br/>
	</xsl:template>

	<xsl:template match="image">
		<img>
			<xsl:attribute name="src">
				<xsl:value-of select="@src"/>
			</xsl:attribute>
		</img>
	</xsl:template>
	
	<xsl:template match="emphasis">
		<i>
			<xsl:value-of select="."/>
		</i>
	</xsl:template>
	
	<xsl:template match="bold">
		<b>
			<xsl:value-of select="."/>
		</b>
	</xsl:template>
	
	<xsl:template match="TBC">
		<div class="{name(.)}">
			<xsl:value-of select="."/>
		</div>
	</xsl:template>

<!-- link -->	
	<xsl:template match="link">
		<a class="pageLink">
			<xsl:attribute name="href">
				<xsl:value-of select="linkURL"/>
			</xsl:attribute>
			<xsl:if test="linkImage">
				<img class="linkImage">
					<xsl:attribute name="src">
						<xsl:value-of select="linkImage"/>
					</xsl:attribute>
				</img>
			</xsl:if>
			<xsl:value-of select="linkName"/>
		</a>
		<xsl:if test="linkDesc">
			<br/>
			<xsl:apply-templates select="linkDesc"/>			
		</xsl:if>
		<xsl:if test="linkDate">
			<xsl:text> - </xsl:text>
			<b>
				<i>
				<xsl:value-of select="linkDate"/>
				</i>
			</b>
		</xsl:if>
		<xsl:if test="linkDesc">
			<br/>
			<br/>
		</xsl:if>
	</xsl:template>
	
<!-- para -->
	<xsl:template match="para">
		<p>
			<xsl:if test="@align">
				<xsl:attribute name="align">
					<xsl:value-of select="@align"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
		</p>
	</xsl:template>

<!-- table related \-->	
	<xsl:template match="table">
		<table>
			<xsl:if test="@align">
				<xsl:attribute name="align">
					<xsl:value-of select="@align"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@valign">
				<xsl:attribute name="valign">
					<xsl:value-of select="@valign"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@width">
				<xsl:attribute name="width">
					<xsl:value-of select="@width"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@height">
				<xsl:attribute name="height">
					<xsl:value-of select="@height"/>
				</xsl:attribute>
			</xsl:if>
      <xsl:if test="@border">
        <xsl:attribute name="border">
          <xsl:value-of select="@border"/>
        </xsl:attribute>
      </xsl:if>

      <xsl:apply-templates select="row"/>
		</table>
	</xsl:template>

	<xsl:template match="row">
		<tr>
			<xsl:if test="@color">
				<xsl:attribute name="style">
          <xsl:text>background-color: </xsl:text>
          <xsl:value-of select="@color"/>
        </xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="entry"/>
		</tr>
	</xsl:template>

	<xsl:template match="entry">
		<td>
			<xsl:if test="@align">
				<xsl:attribute name="align">
					<xsl:value-of select="@align"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@valign">
				<xsl:attribute name="valign">
					<xsl:value-of select="@valign"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@width">
				<xsl:attribute name="width">
					<xsl:value-of select="@width"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@height">
				<xsl:attribute name="height">
					<xsl:value-of select="@height"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@colspan">
				<xsl:attribute name="colspan">
					<xsl:value-of select="@colspan"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@rowspan">
				<xsl:attribute name="rowspan">
					<xsl:value-of select="@rowspan"/>
				</xsl:attribute>
			</xsl:if>

			<xsl:apply-templates/>
		</td>
	</xsl:template>

<!-- news related -->
	<xsl:template match="news">
		<div class="divNews">
			<xsl:if test="@width">
				<xsl:attribute name="width">
					<xsl:value-of select="@width"/>
				</xsl:attribute>
			</xsl:if>
			<div class="divNewsHeader">
				<b><xsl:value-of select="@title"/></b>
				<xsl:if test="@date">
					<br/>
					<i><xsl:apply-templates select="@date"/></i>
				</xsl:if>
			</div>
			<div class="divNewsBody">
				<xsl:apply-templates/>
			</div>
		</div>
	</xsl:template>

<!-- various formating -->
	<xsl:template match="filename">
		<code class="filename">
			<xsl:apply-templates/>
		</code>
	</xsl:template>
	
	<xsl:template match="quote">
		<xsl:text>&quot;</xsl:text>
			<xsl:apply-templates/>
		<xsl:text>&quot;</xsl:text>
	</xsl:template>
	
	<xsl:template match="application">
		<code class="simpleCode">
			<xsl:apply-templates/>
		</code>
	</xsl:template>

	<xsl:template match="programlisting">
		<pre class="programlisting">
			<xsl:apply-templates/>
		</pre>
	</xsl:template>	

	<xsl:template match="computeroutput">
		<code class="simpleCode">
			<xsl:apply-templates/>
		</code>
	</xsl:template>

<!-- procedure, step and substeps templates -->
	<xsl:template match="procedure">
		<xsl:choose>
			<xsl:when test="count(step) = 1">
				<ul>
					<xsl:apply-templates select="step"/>
				</ul>
			</xsl:when>
			<xsl:otherwise>
				<ol>
					<xsl:apply-templates select="step"/>
				</ol>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	
	<xsl:template match="step">
		<li>
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	<xsl:template match="substeps">
		<ul>
			<xsl:apply-templates/>
		</ul>
	</xsl:template>
	
<!-- lists: itemizedlist and listitem templates -->
	<xsl:template match="itemizedlist">
		<div class="{name(.)}">
			<ul>
				<xsl:apply-templates select="listitem"/>
			</ul>
		</div>
	</xsl:template>
	<xsl:template match="listitem">
		<li>
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	
<!-- important, caution, note, tip and warning templates -->
	<xsl:template match="important">
		<div class="genNote">
			<p class="genNoteTitle">
				<xsl:text>Important</xsl:text>
				<xsl:if test="title">
					<xsl:text>: </xsl:text>
					<xsl:value-of select="title"/>
				</xsl:if>
			</p>
			<xsl:apply-templates select="*[not(self::title)]"/>
		</div>
	</xsl:template>
	
	<xsl:template match="caution">
		<div class="genNote">
			<p class="genNoteTitleRed">
				<xsl:text>Caution</xsl:text>
				<xsl:if test="title">
					<xsl:text>: </xsl:text>
					<xsl:value-of select="title"/>
				</xsl:if>
			</p>
			<xsl:apply-templates select="*[not(self::title)]"/>
		</div>
	</xsl:template>
	
	<xsl:template match="note">
		<div class="genNote">
			<p class="genNoteTitle">
				<xsl:text>Note</xsl:text>
				<xsl:if test="title">
					<xsl:text>: </xsl:text>
					<xsl:value-of select="title"/>
				</xsl:if>
			</p>
			<xsl:apply-templates select="*[not(self::title)]"/>
		</div>
	</xsl:template>
	
	<xsl:template match="tip">
		<div class="genNote">
			<p class="genNoteTitle">
				<xsl:text>Tip</xsl:text>
				<xsl:if test="title">
					<xsl:text>: </xsl:text>
					<xsl:value-of select="title"/>
				</xsl:if>
			</p>
			<xsl:apply-templates select="*[not(self::title)]"/>
		</div>
	</xsl:template>
	
	<xsl:template match="warning">
		<div class="genNote">
			<p class="genNoteTitleRed">
				<xsl:text>Warning</xsl:text>
				<xsl:if test="title">
					<xsl:text>: </xsl:text>
					<xsl:value-of select="title"/>
				</xsl:if>
			</p>
			<xsl:apply-templates select="*[not(self::title)]"/>
		</div>
	</xsl:template>

</xsl:stylesheet>